package com.vcms.controller.customer;

import com.vcms.entity.store.item.DrinksStoreItem;
import com.vcms.entity.store.itemtype.DrinksBrand;
import com.vcms.exception.VMCSException;
import com.vcms.patterns.observer.Observable;
import com.vcms.patterns.observer.Observer;

public class DispenseController implements Observer {

	private TransactionController transactionController;

	public DispenseController(TransactionController transactionController) {
		this.transactionController = transactionController;
	}

	public void resetCan() {
		transactionController.getCustomerPanel().getCollectCan().setValue("No CAN");
	}

	public void allowSelection(boolean allow) {
		/*
		if (allow) {
			Store store = transactionController.getMainControler().getStoreController().getStore(StoreController.DRINK);
			StoreItem[] storeItems = store.getItems();
			for (int i = 0; i < storeItems.length; i++) {
				if (storeItems[i].getQuantity() <= 0)
					transactionController.getCustomerPanel().getDrinkInput().setItemState(i, false);
				else
					transactionController.getCustomerPanel().getDrinkInput().setItemState(i, true);
			}
		} else {
			if (transactionController.getCustomerPanel() != null) {
				transactionController.getCustomerPanel().getDrinkInput().setActive(allow);
			}
		}
		*/
		if (transactionController.getCustomerPanel() != null) {
			transactionController.getCustomerPanel().getDrinkInput().setActive(allow);
		}
	}

	public boolean dispenseDrink(int selection) throws VMCSException {
		try {
			transactionController.getMainController().getMachinaryController().dispenseDrink(selection);
		} catch (VMCSException e) {
			transactionController.terminateFault(this);
			throw new VMCSException("Dispense Drinks", e.getMessage());
		}
//		DrinksStore drinksStore = (DrinksStore) transactionController.getMainControler().getStoreController().getStore(StoreController.DRINK);
//		DrinksStoreItem drinksStoreItem = (DrinksStoreItem) drinksStore.getStoreItem(selection);
//		DrinksBrand drinksBrand = (DrinksBrand) drinksStoreItem.getContent();
//		transactionController.getCustomerPanel().getCollectCan().setValue(drinksBrand.getName());
		
//		updateDrinkSelection(selection);
		return true;
	}
	
//	private void updateDrinkSelection(int selection){
//		try {
//			DrinksStore drinksStore =(DrinksStore)transactionController.getMainControler().getStoreController().getStore(StoreController.DRINK);
//			DrinksStoreItem drinksStoreItem = (DrinksStoreItem)drinksStore.getStoreItem(selection);
//			DrinksBrand drinksBrand = (DrinksBrand)drinksStoreItem.getContent();
//			
//			transactionController.getCustomerPanel().getDrinkInput().update(selection, drinksStoreItem.getQuantity(), drinksBrand.getPrice(), drinksBrand.getName()) ;
//		} catch (VMCSException e) {
//			e.printStackTrace();
//		}
//	}

	@Override
	public void update(Observable observable, Object object, int selection) {
		DrinksBrand drinksBrand = (DrinksBrand)object;
		DrinksStoreItem drinksStoreItem = (DrinksStoreItem)observable;
		transactionController.getCustomerPanel().getCollectCan().setValue(drinksBrand.getName());
		transactionController.getCustomerPanel().getDrinkInput().update(selection, drinksStoreItem.getQuantity(), drinksBrand.getPrice(), drinksBrand.getName()) ;
		
		System.out.println("DispenseController - update");
	}

}
